package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.base.aircraft.*; 
import java.awt.Point;
import javax.swing.JOptionPane;
import com.hitechbangla.fctu.base.Location;
import com.hitechbangla.fctu.utility.GeneralUtil;
import com.hitechbangla.fctu.utility.RecordedGame;
import fctu.simulator.HTB_MAIN;
import java.awt.MouseInfo;
import java.awt.geom.Point2D;
import org.openstreetmap.gui.jmapviewer.OsmMercator;

/**
 *
 * @author Asus
 */
public class AddAircraft extends javax.swing.JDialog
{
    /**
     * Creates new form AddAircraft
     */
    
    //Class instance variable
    private GameScene gameScene;
    private Point2D point;
    private String[] aircraftTypeString;
    private com.hitechbangla.fctu.base.aircraft.Aircraft aircraft;
    private double lon;
    private double lat;
        
    AircraftProperty acp =  new AircraftProperty();
    
    public AddAircraft(GameScene gameScene, Point2D point1) {
        this.gameScene = gameScene;
	this.point = point1;	
        
	this.lon = OsmMercator.XToLon((int)(point.getX() + HTB_MAIN.translate_x), HTB_MAIN.currentZoom);
        this.lat = OsmMercator.YToLat((int)(-(point.getY() - HTB_MAIN.translate_y)), HTB_MAIN.currentZoom);
        
              this.setLocation(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
        
              aircraftTypeString = new String[gameScene.airCraftTypeList.size()];
              
              for(int i=0;i<aircraftTypeString.length;i++)
              {
                  aircraftTypeString[i] = gameScene.airCraftTypeList.get(i).getTypename();
                  //System.out.println(aircraftTypeString[i]);
              }
              
               initComponents();
               setModalityType(ModalityType.APPLICATION_MODAL);
               jComboBox1ActionPerformed(null);
               //this.getParent().setEnabled(false);
               //comment out by saiful abu
               //note to moon vai fix the bug
	   // this.jTextFieldFuelStatus.setText(String.valueOf(HTB_MAIN.gameScene.airCraftTypeList.get(jComboBox1.getSelectedIndex()).getFuelCapacity())); 
       // this.setClosable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextFieldCallSign = new javax.swing.JTextField();
        jTextFieldFuelStatus = new javax.swing.JTextField();
        jTextFieldHeading = new javax.swing.JTextField();
        jTextFieldLocationX = new javax.swing.JTextField();
        jTextFieldLocationY = new javax.swing.JTextField();
        jTextFieldLocationZ = new javax.swing.JTextField();
        jTextFieldSpeed = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButtonOk = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Aircraft");
        setAlwaysOnTop(true);
        setType(java.awt.Window.Type.UTILITY);

        if(aircraftTypeString!=null)
        {
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(aircraftTypeString));
        }
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jTextFieldCallSign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCallSignActionPerformed(evt);
            }
        });

        jTextFieldLocationX.setText(String.valueOf(this.lon));

        jTextFieldLocationY.setText(String.valueOf(this.lat));

        jLabel1.setText("Aircraft Type:");

        jLabel2.setText("Fuel Status:");

        jLabel3.setText("Call Sign:");

        jLabel4.setText("Heading:");

        jLabel5.setText("Location (lon):");

        jLabel6.setText("Location (lat):");

        jLabel8.setText("Location (alt):");

        jLabel9.setText("Speed:");

        jButtonOk.setText("Ok");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldCallSign)
                            .addComponent(jTextFieldFuelStatus)
                            .addComponent(jTextFieldHeading)
                            .addComponent(jTextFieldLocationX)
                            .addComponent(jTextFieldLocationY)
                            .addComponent(jTextFieldLocationZ)
                            .addComponent(jTextFieldSpeed)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 72, Short.MAX_VALUE))))
                .addGap(25, 25, 25))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCallSign, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldFuelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldHeading, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldLocationX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldLocationY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldLocationZ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldSpeed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOk)
                    .addComponent(jButtonCancel))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jLabel5.getAccessibleContext().setAccessibleName("Location (lon):");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldCallSignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCallSignActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldCallSignActionPerformed

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        if(checkTextFieldsEmpty())
        {
            JOptionPane.showMessageDialog(this, "Enter all the fields!");
        }
        else
        {
            double fLon = Double.parseDouble(jTextFieldLocationX.getText());
            double fLat = Double.parseDouble(jTextFieldLocationY.getText());
            
            int fX = OsmMercator.LonToX(fLon, HTB_MAIN.currentZoom);
            int fY = OsmMercator.LatToY(fLat, HTB_MAIN.currentZoom);
            
            double x = (fX - HTB_MAIN.translate_x);
	    double y = (-fY + HTB_MAIN.translate_y);
            
            Location location = new Location(x *gameScene.HTBParent.one_nm_equivalent_pixel , y * gameScene.HTBParent.one_nm_equivalent_pixel, Double.parseDouble(jTextFieldLocationZ.getText()));
            AircraftType selectedAircraftType = gameScene.airCraftTypeList.get(jComboBox1.getSelectedIndex());
            
            //float nm_system_x = Float.parseFloat(jTextFieldX.getText()) * gameScene.HTBParent.one_nm_equivalent_pixel;
            //float nm_system_y = Float.parseFloat(jTextFieldY.getText()) * gameScene.HTBParent.one_nm_equivalent_pixel;
            
            AircraftState aircraftState = new AircraftState(jTextFieldCallSign.getText(),location,Float.parseFloat(jTextFieldHeading.getText()), Float.parseFloat(jTextFieldSpeed.getText()), selectedAircraftType.getFuelCapacity());
	    aircraftState.getListLocation().add(location);
            aircraft = new com.hitechbangla.fctu.base.aircraft.Aircraft(aircraftState, selectedAircraftType);
            gameScene.airCraftList.add(aircraft); 
            gameScene.HTBParent.repaint();
            this.setVisible(false);
            dispose();
            
            GeneralUtil.UpdateGameScene(gameScene);
	    
	    RecordedGame recordedGame = gameScene.HTBParent.getRecordedGame();
	    
	    if(recordedGame!=null)
	    {
		if(recordedGame.isRecordingStarted())
		{
		    recordedGame.storeGameScene(gameScene);
		}
	    }	    
        }
        
    }//GEN-LAST:event_jButtonOkActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        this.setVisible(false);
        dispose();        
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        this.jTextFieldFuelStatus.setText(String.valueOf(HTB_MAIN.gameScene.airCraftTypeList.get(jComboBox1.getSelectedIndex()).getFuelCapacity())); 
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private boolean checkTextFieldsEmpty()
    {
        if(jTextFieldCallSign.getText().trim().equals("") || jTextFieldFuelStatus.getText().trim().equals("") || jTextFieldHeading.getText().trim().equals("") ||  jTextFieldLocationZ.getText().trim().equals("") || jTextFieldSpeed.getText().trim().equals(""))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } 
        catch (ClassNotFoundException ex) 
        {
            java.util.logging.Logger.getLogger(AddAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (InstantiationException ex) 
        {
            java.util.logging.Logger.getLogger(AddAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (IllegalAccessException ex) 
        {
            java.util.logging.Logger.getLogger(AddAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 
        catch (javax.swing.UnsupportedLookAndFeelException ex) 
        {
            java.util.logging.Logger.getLogger(AddAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
	Location location = new Location(2, 2, 2);
        AircraftState aircraftState = new AircraftState("222", location, 34, 1200, 2000);
	aircraftState.getListLocation().add(location);
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldCallSign;
    private javax.swing.JTextField jTextFieldFuelStatus;
    private javax.swing.JTextField jTextFieldHeading;
    private javax.swing.JTextField jTextFieldLocationX;
    private javax.swing.JTextField jTextFieldLocationY;
    private javax.swing.JTextField jTextFieldLocationZ;
    private javax.swing.JTextField jTextFieldSpeed;
    // End of variables declaration//GEN-END:variables
}
